﻿var express = require('express');
var bodyParser  =  require("body-parser");  
var app = express();

// need it...  
app.use(bodyParser.urlencoded({ extended: false }));  
app.all('*',function (req, res,next) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  next();
  
})

app.get('/', function (req, res) {
  res.send('Hello World!');
});
app.get('/info', function (req, res) {
    
  console.log('jj')
  res.setHeader("Access-Control-Allow-Origin", "*");

  res.type('application/json');
  var info;
  console.log(req.query)
  if(req.query.id == 1){
      info = {name:'admin',age:16}
  }else{
      info = {name:'fangke',age:'fangke'}
  }
  
  
  res.json(info);
});
app.get('/infos/:id', function (req, res) {
    
  console.log(req.params.id)
  res.setHeader("Access-Control-Allow-Origin", "*");

  var info = {name:'fangke',age:'fangke'}

  
  res.json(info);
});
app.post('/infos/:id', function (req, res) {
    
  
  res.setHeader("Access-Control-Allow-Origin", "*");
    
  if(req.params.id==1){
      console.log('success')
          var newInfo = {
          name:req.body.name,
          age:req.body.age
      }
  }
  var info = [];
  
    
  info.push(newInfo)
  
  res.json(info);
});

app.get('/messagesssss', function (req, res) {
 
  res.setHeader("Access-Control-Allow-Origin", "*");
  //res.setHeader("MockXMLHttpRequest", "*");
  var dataid = req.query.dataid;
  console.log(req.query);
  res.type('application/json');

  var info = {
    qita:[
        {infos:'核医学科'},
        {infos:'男科'},
        {infos:'口腔额外科'},
        {infos:'超声诊断科'},
        {infos:'放疗科'},
        {infos:'肿瘤内科'},
        {infos:'介入治疗科'},
        {infos:'口腔内科'},
        {infos:'口腔正畸'},
        {infos:'心理学科'},
        {infos:'营养科'},
        {infos:'疼痛科'},
        {infos:'病理科'},
        {infos:'放射科'},
        {infos:'口腔修复'},
        {infos:'麻醉科'},
        {infos:'口腔黏膜'},
        {infos:'口腔牙周'},
        {infos:'针灸科'},
        {infos:'重症医学科'},
        {infos:'体检中心'},
        {infos:'物理医学康复科'},
        {infos:'口腔中种植'},
        {infos:'中医正骨'},
        {infos:'体检科'},
        {infos:'临床药理中心'},
        {infos:'卫干门诊'},
        {infos:'药剂科'},
        {infos:'妇科肿瘤'},
        {infos:'眼科'},
        {infos:'耳喉科'},
        {infos:'皮肤科'},
        {infos:'中医科'}
    ],
    fuke:[
      {infos:'妇泌尿'},
      {infos:'产科'},
      {infos:'计划生育'},
      {infos:'生殖内分泌'},
      {infos:'儿科'},
      {infos:'妇科'}
    ],
    waike:[
      {infos:'乳腺外科'},{infos:'胸外科'},{infos:'血管外科中心'},{infos:'肝脏外科'},{infos:'心外科'},{infos:'整形外科'},{infos:'泌尿外科'},{infos:'圣经外科'},{infos:'骨科'},{infos:'神经外科'},{infos:'基本外科'},{infos:'整形外科'}
    ],
    neike:[{infos:'变态反应科'},{infos:'心内科'},{infos:'肾内科'},{infos:'感染内科'},{infos:'普通内科'},{infos:'老年医学科'},{infos:'急诊科'},{infos:'呼吸内科'},{infos:'血液内科'},{infos:'内分泌科'},{infos:'神经科'},{infos:'风湿免疫科'},{infos:'消化内科'}]
  };
  res.json( info[dataid] );
});


app.get('/message', function (req, res) {
 
  res.setHeader("Access-Control-Allow-Origin", "*");

  res.type('application/json');
  
  var info = [
   {
    qita:[
        {infos:'核医学科'},{infos:'男科'}, {infos:'口腔额外科'},{infos:'超声诊断科'},{infos:'放疗科'},{infos:'肿瘤内科'},{infos:'介入治疗科'},{infos:'口腔内科'},{infos:'口腔正畸'},{infos:'心理学科'},{infos:'营养科'},{infos:'疼痛科'},{infos:'病理科'},{infos:'放射科'},{infos:'口腔修复'},{infos:'麻醉科'},{infos:'口腔黏膜'},{infos:'口腔牙周'},{infos:'针灸科'},{infos:'重症医学科'},{infos:'体检中心'},{infos:'物理医学康复科'},{infos:'口腔中种植'},{infos:'中医正骨'},{infos:'体检科'},{infos:'临床药理中心'},{infos:'卫干门诊'},{infos:'药剂科'},{infos:'妇科肿瘤'},{infos:'眼科'},{infos:'耳喉科'},{infos:'皮肤科'},{infos:'中医科'}
    ]},
    {
      fu:[{infos:'妇泌尿'},{infos:'产科'},{infos:'计划生育'},{infos:'生殖内分泌'},{infos:'儿科'},{infos:'妇科'}]
    },
    {
      wai:[{infos:'乳腺外科'},{infos:'胸外科'},{infos:'血管外科中心'},{infos:'肝脏外科'},{infos:'心外科'},{infos:'整形外科'},{infos:'泌尿外科'},{infos:'圣经外科'},{infos:'骨科'},{infos:'神经外科'},{infos:'基本外科'},{infos:'整形外科'}]
    },
    {
      nei:[{infos:'变态反应科'},{infos:'心内科'},{infos:'肾内科'},{infos:'感染内科'},{infos:'普通内科'},{infos:'老年医学科'},{infos:'急诊科'},{infos:'呼吸内科'},{infos:'血液内科'},{infos:'内分泌科'},{infos:'神经科'},{infos:'风湿免疫科'},{infos:'消化内科'}]
    }
  ];
  var newInfo = info.filter(function(x){
      return x.name === req.query.name && x;
  })
  res.json(newInfo);
});


app.get('/welcome', function (req, res) {
  //var name = req.query.name;

  //res.setHeader("Access-Control-Allow-Origin", "*");
  //res.type('application/json');
  
  //res.send('welcome, '+ name);
  
  var name = req.query.name,age = req.query.age;

  //res.setHeader("Access-Control-Allow-Origin", "*");
  //res.type('application/json');
  
  res.send('I am, '+ name + ' I\'m ' + age + ' years old');
});
app.get('/getHtml', function (req, res) {

  res.setHeader("Access-Control-Allow-Origin", "*");
  res.type('.html');
 
  res.sendFile(__dirname + '/jqueryAjax-copy.html');
});
app.get('/getJson', function (req, res) {

  //res.setHeader("Access-Control-Allow-Origin", "*");
 
  res.sendFile(__dirname + '/j.js');
});
app.post('/saveInfo', function (req, res) {
    //console.log('saveInfo')
   res.type('application/json');
   res.setHeader("Access-Control-Allow-Origin", "*");

  console.log(req.body)
  var name = req.body.name,
        age = req.body.age;
    console.log(name + ' ' + age);
  var json = {
      name:name,
      age:age
  }
  res.json(json);
});
app.get('/vit',function  (req,res,next) {
    res.type('application/json');
    res.jsonp({'name':'lilei','age':'34'});  //items is the object
});
app.listen(9999,'127.0.0.1', function () {
  console.log('Example app listening on port 9999!');
});


